package com.young.sso.controller;

import com.young.sso.Service.UserService;
import com.young.sso.common.JsonResult;
import com.young.sso.entity.User;
import com.young.sso.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * 用户执行一键登录或注册的接口
 *
 * @author young
 * @date 2021/11/30 9:20
 */
@RestController
@RequestMapping("/user")
@Api(tags = "登录")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "string", paramType = "query")
    })
    @ApiOperation("手机号验证码登录")
    @GetMapping("/loginByCode")
    public Map<String, Object> loginByCode(String phone, String code) throws BusinessException {
        Map<String, Object> login = userService.login(phone, code);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("登录成功");
        jsonResult.setDataMap(login);
        return jsonResult.getDataMap();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "string", paramType = "query")
    })
    @ApiOperation("手机号密码登录")
    @GetMapping("/loginByPassword")
    public Map<String, Object> loginByPassword(String phone, String password) throws BusinessException {
        Map<String, Object> login = userService.loginByPassword(phone, password);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("登录成功");
        jsonResult.put("token", login.get("token"));
        jsonResult.put("user", login.get("user"));
        return jsonResult.getDataMap();
    }


    @ApiImplicitParam(name = "token", value = "token", dataType = "string", required = true, paramType = "query")
    @PostMapping("/queryUserByToken")
    @ApiOperation("服务器之间验证token")
    public User queryUserByToken(String token) throws BusinessException {
        return userService.queryUserByToken(token);
    }


    @PostMapping("/reg")
    @ApiOperation("注册")
    public Map<String, Object> reg(@RequestBody User user) throws Exception {
        userService.addUser(user);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("注册成功");
        return jsonResult.getDataMap();
    }


    @PostMapping("/logout")
    @ApiOperation("登出")
    public Map<String, Object> logout(String token) throws Exception {
        JsonResult jsonResult = new JsonResult();
        Boolean delete = redisTemplate.delete(token);
        if (delete) {

            jsonResult.setCode("200");
            jsonResult.setMsg("退出登录");

        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("操作失败");
        }
        return jsonResult.getDataMap();
    }

}
